package com.whmskj.xjlhsc.gongweiji.produce.service.imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.whmskj.xjlhsc.gongweiji.produce.mapper.GwjemployeeperformanceMapper;
import com.whmskj.xjlhsc.gongweiji.produce.pojo.dto.*;
import com.whmskj.xjlhsc.gongweiji.produce.pojo.vo.*;
import com.whmskj.xjlhsc.gongweiji.produce.service.IGwjemployeeperformanceService;
import com.whmskj.xjlhsc.gongweiji.produce.service.MonthJiXiaomonthlyWorktimeDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.util.Collections;
import java.util.List;

/**
 * @author jiaruchun
 * @date 2025-09-27 16:04
 */
@Service
public class GwjemployeeperformanceServiceImpl implements IGwjemployeeperformanceService {
    @Autowired
    private GwjemployeeperformanceMapper gwjemployeeperformanceMapper;

    @Override
    public PageResult<DailyJiXiaoVO> daily(DailyJiXiaoDTO dto) {
        PageHelper.startPage(dto.getPageNum() == null ? 1 : dto.getPageNum(), dto.getPageSize() == null ? 10 : dto.getPageSize());
        List<DailyJiXiaoVO> daily = gwjemployeeperformanceMapper.daily(dto.getDate(), dto.getProcess(), dto.getWorkType(), dto.getKeyword());
        Page<DailyJiXiaoVO> daily1 = (Page<DailyJiXiaoVO>) daily;
        //daily倒转
        Collections.reverse(daily);
        return new PageResult<>(daily1.getTotal(),daily1.getResult());
    }

    @Override
    public List<WorkTypeVO> workType() {
        return gwjemployeeperformanceMapper.workType();
    }

    @Override
    public PageResult<GwjemployeeperformanceMonthVO> monthly(MonthJiXiaoDTO dto) {
        String month = dto.getMonth(); // yyyy-MM
        String start;
        String end;
        Boolean isEndDate20th = false;

        LocalDateTime now = LocalDateTime.now();
        YearMonth yearMonth;
        if (month == null || month.isEmpty()) {
            yearMonth = YearMonth.from(now);
            month = yearMonth.toString();
        } else {
            yearMonth = YearMonth.parse(month); // 解析 yyyy-MM
        }

// 当前月的20号 21:30
        LocalDateTime curMonth20_2130 = yearMonth.atDay(20).atTime(21, 30);

// 上个月的20号
        LocalDate prevMonth20 = yearMonth.minusMonths(1).atDay(20);

        if (yearMonth.equals(YearMonth.from(now))) {
            // 如果传入的month是当前月
            if (now.isAfter(curMonth20_2130)) {
                // 已经过了本月20号21:30
                end = now.toLocalDate().toString();
                start = yearMonth.atDay(20).toString();
            } else {
                // 还没到本月20号21:30
                end = yearMonth.atDay(20).toString();
                start = prevMonth20.toString();
                isEndDate20th = true;
            }
        } else {
            // 非当前月
            start = prevMonth20.toString();
            end = yearMonth.atDay(20).toString();
            isEndDate20th = true;
        }
        PageHelper.startPage(dto.getPageNum() == null || dto.getPageNum().toString().isEmpty() ? 1 : Integer.parseInt(dto.getPageNum().toString()), (dto.getPageSize() == null || dto.getPageSize().toString().isEmpty()) ? 10 : Integer.parseInt(dto.getPageSize().toString()));
        List<GwjemployeeperformanceMonthVO> gwjemployeeperformanceMonthVOS = gwjemployeeperformanceMapper.monthly(start, end, dto.getProcess(), dto.getWorkType(), dto.getKeyword(), isEndDate20th);
        Page<GwjemployeeperformanceMonthVO> gwjemployeeperformanceMonthVOS1 = (Page<GwjemployeeperformanceMonthVO>) gwjemployeeperformanceMonthVOS;
        for (GwjemployeeperformanceMonthVO gwjemployeeperformanceMonthVO : gwjemployeeperformanceMonthVOS1) {
            gwjemployeeperformanceMonthVO.setMonth(month);
        }
        return new PageResult<>(gwjemployeeperformanceMonthVOS1.getTotal(),gwjemployeeperformanceMonthVOS1.getResult());
    }


    @Override
    public PageResult<GwjemployeeperformanceMonthDeVO> monthlyDetail(MonthJiXiaoDetailDTO dto) {
        String month = dto.getMonth(); // yyyy-MM
        String start;
        String end;
        Boolean isEndDate20th = false;

        LocalDateTime now = LocalDateTime.now();
        YearMonth yearMonth;
        if (month == null || month.isEmpty()) {
            yearMonth = YearMonth.from(now);
            month = yearMonth.toString();
        } else {
            yearMonth = YearMonth.parse(month); // 解析 yyyy-MM
        }

// 当前月的20号 21:30
        LocalDateTime curMonth20_2130 = yearMonth.atDay(20).atTime(21, 30);

// 上个月的20号
        LocalDate prevMonth20 = yearMonth.minusMonths(1).atDay(20);

        if (yearMonth.equals(YearMonth.from(now))) {
            // 如果传入的month是当前月
            if (now.isAfter(curMonth20_2130)) {
                // 已经过了本月20号21:30
                end = now.toLocalDate().toString();
                start = yearMonth.atDay(20).toString();
            } else {
                // 还没到本月20号21:30
                end = yearMonth.atDay(20).toString();
                start = prevMonth20.toString();
                isEndDate20th = true;
            }
        } else {
            // 非当前月
            start = prevMonth20.toString();
            end = yearMonth.atDay(20).toString();
            isEndDate20th = true;
        }
        PageHelper.startPage(dto.getPageNum() == null || dto.getPageNum().toString().isEmpty() ? 1 : Integer.parseInt(dto.getPageNum().toString()), (dto.getPageSize() == null || dto.getPageSize().toString().isEmpty()) ? 10 : Integer.parseInt(dto.getPageSize().toString()));
        List<GwjemployeeperformanceMonthDeVO> gwjemployeeperformanceMonthDeVOS = gwjemployeeperformanceMapper.monthlyDetail(start, end, dto.getEmployeeId(), isEndDate20th);
        Page<GwjemployeeperformanceMonthDeVO> gwjemployeeperformanceMonthDeVOS1 = (Page<GwjemployeeperformanceMonthDeVO>) gwjemployeeperformanceMonthDeVOS;
        for (GwjemployeeperformanceMonthDeVO gwjemployeeperformanceMonthDeVO : gwjemployeeperformanceMonthDeVOS1) {
            gwjemployeeperformanceMonthDeVO.setMonth(month);
        }
        return new PageResult<>(gwjemployeeperformanceMonthDeVOS1.getTotal(),gwjemployeeperformanceMonthDeVOS1.getResult());
    }

    /**
     * @param dto
     * @return
     */
    @Override
    public PageResult<MonthJiXiaomonthlyYieldVO> monthlyYield(MonthJiXiaomonthlyYieldDTO dto) {
        PageHelper.startPage((dto.getPageNum() == null || dto.getPageNum().toString().isEmpty()) ? 1 : Integer.parseInt(dto.getPageNum().toString()), dto.getPageSize() == null ? 10 : dto.getPageSize());

        String month = dto.getMonth(); // yyyy-MM
        String start;
        String end;
        Boolean isEndDate20th = false;

        LocalDateTime now = LocalDateTime.now();
        YearMonth yearMonth;
        if (month == null || month.isEmpty()) {
            yearMonth = YearMonth.from(now);
            month = yearMonth.toString();
        } else {
            yearMonth = YearMonth.parse(month); // 解析 yyyy-MM
        }

// 当前月的20号 21:30
        LocalDateTime curMonth20_2130 = yearMonth.atDay(20).atTime(21, 30);

// 上个月的20号
        LocalDate prevMonth20 = yearMonth.minusMonths(1).atDay(20);

        if (yearMonth.equals(YearMonth.from(now))) {
            // 如果传入的month是当前月
            if (now.isAfter(curMonth20_2130)) {
                // 已经过了本月20号21:30
                end = now.toLocalDate().toString();
                start = yearMonth.atDay(20).toString();
            } else {
                // 还没到本月20号21:30
                end = yearMonth.atDay(20).toString();
                start = prevMonth20.toString();
                isEndDate20th = true;
            }
        } else {
            // 非当前月
            start = prevMonth20.toString();
            end = yearMonth.atDay(20).toString();
            isEndDate20th = true;
        }
        PageHelper.startPage(dto.getPageNum() == null || dto.getPageNum().toString().isEmpty() ? 1 : Integer.parseInt(dto.getPageNum().toString()), dto.getPageSize() == null ? 10 : dto.getPageSize());
        List<MonthJiXiaomonthlyYieldVO> monthJiXiaomonthlyYieldVOS = gwjemployeeperformanceMapper.monthlyYield(start, end, dto.getProcess(), dto.getWorkType(), dto.getKeyword(), dto.getShift(), isEndDate20th);
        Page<MonthJiXiaomonthlyYieldVO> monthJiXiaomonthlyYieldVOS1 = (Page<MonthJiXiaomonthlyYieldVO>) monthJiXiaomonthlyYieldVOS;
        for (MonthJiXiaomonthlyYieldVO monthJiXiaomonthlyYieldVO : monthJiXiaomonthlyYieldVOS1) {
            monthJiXiaomonthlyYieldVO.setMonth(month);
        }
        return new PageResult<>(monthJiXiaomonthlyYieldVOS1.getTotal(),monthJiXiaomonthlyYieldVOS1.getResult());
    }

    /**
     * @param dto
     * @return
     */
    @Override
    public PageResult<MonthJiXiaomonthlyYieldDeVO> monthlyYieldDetail(MonthJiXiaomonthlyYieldDetailDTO dto) {
        String month = dto.getMonth(); // yyyy-MM
        String start;
        String end;
        Boolean isEndDate20th = false;

        LocalDateTime now = LocalDateTime.now();
        YearMonth yearMonth;
        if (month == null || month.isEmpty()) {
            yearMonth = YearMonth.from(now);
        } else {
            yearMonth = YearMonth.parse(month); // 解析 yyyy-MM
        }

// 当前月的20号 21:30
        LocalDateTime curMonth20_2130 = yearMonth.atDay(20).atTime(21, 30);

// 上个月的20号
        LocalDate prevMonth20 = yearMonth.minusMonths(1).atDay(20);

        if (yearMonth.equals(YearMonth.from(now))) {
            // 如果传入的month是当前月
            if (now.isAfter(curMonth20_2130)) {
                // 已经过了本月20号21:30
                end = now.toLocalDate().toString();
                start = yearMonth.atDay(20).toString();
            } else {
                // 还没到本月20号21:30
                end = yearMonth.atDay(20).toString();
                start = prevMonth20.toString();
                isEndDate20th = true;
            }
        } else {
            // 非当前月
            start = prevMonth20.toString();
            end = yearMonth.atDay(20).toString();
            isEndDate20th = true;
        }
        PageHelper.startPage(dto.getPageNum() == null || dto.getPageNum().toString().isEmpty() ? 1 : Integer.parseInt(dto.getPageNum().toString()), dto.getPageSize() == null ? 10 : dto.getPageSize());
        List<MonthJiXiaomonthlyYieldDeVO> monthJiXiaomonthlyYieldDeVOS = gwjemployeeperformanceMapper.monthlyYieldDetail(start, end, dto.getEmployeeId(), isEndDate20th);
        Page<MonthJiXiaomonthlyYieldDeVO> monthJiXiaomonthlyYieldDeVOS1 = (Page<MonthJiXiaomonthlyYieldDeVO>) monthJiXiaomonthlyYieldDeVOS;
        return new PageResult<>(monthJiXiaomonthlyYieldDeVOS1.getTotal(),monthJiXiaomonthlyYieldDeVOS1.getResult());
    }

    /**
     * @param dto
     * @return
     */
    @Override
    public PageResult<MonthJiXiaomonthlyWorktimeVO> monthlyWorktime(MonthJiXiaomonthlyWorktimeDTO dto) {
        String month = dto.getMonth(); // yyyy-MM
        String start;
        String end;
        Boolean isEndDate20th = false;

        LocalDateTime now = LocalDateTime.now();
        YearMonth yearMonth;
        if (month == null || month.isEmpty()) {
            yearMonth = YearMonth.from(now);
            month = yearMonth.toString();
        } else {
            yearMonth = YearMonth.parse(month); // 解析 yyyy-MM
        }

// 当前月的20号 21:30
        LocalDateTime curMonth20_2130 = yearMonth.atDay(20).atTime(21, 30);

// 上个月的20号
        LocalDate prevMonth20 = yearMonth.minusMonths(1).atDay(20);

        if (yearMonth.equals(YearMonth.from(now))) {
            // 如果传入的month是当前月
            if (now.isAfter(curMonth20_2130)) {
                // 已经过了本月20号21:30
                end = now.toLocalDate().toString();
                start = yearMonth.atDay(20).toString();
            } else {
                // 还没到本月20号21:30
                end = yearMonth.atDay(20).toString();
                start = prevMonth20.toString();
                isEndDate20th = true;
            }
        } else {
            // 非当前月
            start = prevMonth20.toString();
            end = yearMonth.atDay(20).toString();
            isEndDate20th = true;
        }
        PageHelper.startPage(dto.getPageNum() == null || dto.getPageNum().toString().isEmpty() ? 1 : Integer.parseInt(dto.getPageNum().toString()), (dto.getPageSize() == null || dto.getPageSize().toString().isEmpty()) ? 10 : Integer.parseInt(dto.getPageSize().toString()));
        List<MonthJiXiaomonthlyWorktimeVO> monthJiXiaomonthlyWorktimeVOS = gwjemployeeperformanceMapper.monthlyWorktime(start, end, dto.getProcess(), dto.getWorkType(), dto.getKeyword(), dto.getShift(), isEndDate20th);
        Page<MonthJiXiaomonthlyWorktimeVO> monthJiXiaomonthlyWorktimeVOS1 = (Page<MonthJiXiaomonthlyWorktimeVO>) monthJiXiaomonthlyWorktimeVOS;
        for (MonthJiXiaomonthlyWorktimeVO monthJiXiaomonthlyWorktimeVO : monthJiXiaomonthlyWorktimeVOS1) {
            monthJiXiaomonthlyWorktimeVO.setMonth(month);
        }
        return new PageResult<>(monthJiXiaomonthlyWorktimeVOS1.getTotal(),monthJiXiaomonthlyWorktimeVOS1.getResult());
    }

    /**
     * @param dto
     * @return
     */
    @Override
    public PageResult<MonthJiXiaomonthlyWorktimeDetailVO> monthlyWorktimeDetail(MonthJiXiaomonthlyWorktimeDetailDTO dto) {
        String month = dto.getMonth(); // yyyy-MM
        String start;
        String end;
        Boolean isEndDate20th = false;

        LocalDateTime now = LocalDateTime.now();
        YearMonth yearMonth;
        if (month == null || month.isEmpty()) {
            yearMonth = YearMonth.from(now);
        } else {
            yearMonth = YearMonth.parse(month); // 解析 yyyy-MM
        }

// 当前月的20号 21:30
        LocalDateTime curMonth20_2130 = yearMonth.atDay(20).atTime(21, 30);

// 上个月的20号
        LocalDate prevMonth20 = yearMonth.minusMonths(1).atDay(20);

        if (yearMonth.equals(YearMonth.from(now))) {
            // 如果传入的month是当前月
            if (now.isAfter(curMonth20_2130)) {
                // 已经过了本月20号21:30
                end = now.toLocalDate().toString();
                start = yearMonth.atDay(20).toString();
            } else {
                // 还没到本月20号21:30
                end = yearMonth.atDay(20).toString();
                start = prevMonth20.toString();
                isEndDate20th = true;
            }
        } else {
            // 非当前月
            start = prevMonth20.toString();
            end = yearMonth.atDay(20).toString();
            isEndDate20th = true;
        }
        PageHelper.startPage(dto.getPageNum() == null || dto.getPageNum().toString().isEmpty() ? 1 : Integer.parseInt(dto.getPageNum().toString()), (dto.getPageSize() == null || dto.getPageSize().toString().isEmpty()) ? 10 : Integer.parseInt(dto.getPageSize().toString()));
        List<MonthJiXiaomonthlyWorktimeDetailVO> monthJiXiaomonthlyWorktimeDetailVOS = gwjemployeeperformanceMapper.monthlyWorktimeDetail(start, end, dto.getEmployeeId(), isEndDate20th);
        Page<MonthJiXiaomonthlyWorktimeDetailVO> monthJiXiaomonthlyWorktimeDetailVOS1 = (Page<MonthJiXiaomonthlyWorktimeDetailVO>) monthJiXiaomonthlyWorktimeDetailVOS;
        return new PageResult<>(monthJiXiaomonthlyWorktimeDetailVOS1.getTotal(),monthJiXiaomonthlyWorktimeDetailVOS1.getResult());
    }

    /**
     * @param dto
     * @return
     */
    @Override
    public PageResult<MonthJiXiaomonthlyAccuratePayVO> monthlyAccuratePay(MonthJiXiaomonthlyAccuratePayDTO dto) {
        String month = dto.getMonth(); // yyyy-MM
        String start;
        String end;
        Boolean isEndDate20th = false;

        LocalDateTime now = LocalDateTime.now();
        YearMonth yearMonth;
        if (month == null || month.isEmpty()) {
            yearMonth = YearMonth.from(now);
            month = yearMonth.toString();
        } else {
            yearMonth = YearMonth.parse(month); // 解析 yyyy-MM
        }

// 当前月的20号 21:30
        LocalDateTime curMonth20_2130 = yearMonth.atDay(20).atTime(21, 30);

// 上个月的20号
        LocalDate prevMonth20 = yearMonth.minusMonths(1).atDay(20);

        if (yearMonth.equals(YearMonth.from(now))) {
            // 如果传入的month是当前月
            if (now.isAfter(curMonth20_2130)) {
                // 已经过了本月20号21:30
                end = now.toLocalDate().toString();
                start = yearMonth.atDay(20).toString();
            } else {
                // 还没到本月20号21:30
                end = yearMonth.atDay(20).toString();
                start = prevMonth20.toString();
                isEndDate20th = true;
            }
        } else {
            // 非当前月
            start = prevMonth20.toString();
            end = yearMonth.atDay(20).toString();
            isEndDate20th = true;
        }
        PageHelper.startPage(dto.getPageNum() == null || dto.getPageNum().toString().isEmpty() ? 1 : Integer.parseInt(dto.getPageNum().toString()), (dto.getPageSize() == null || dto.getPageSize().toString().isEmpty()) ? 10 : Integer.parseInt(dto.getPageSize().toString()));
        List<MonthJiXiaomonthlyAccuratePayVO> monthJiXiaomonthlyAccuratePayVOS = gwjemployeeperformanceMapper.monthlyAccuratePay(start, end, dto.getKeyword(), isEndDate20th, dto.getProcess());
        Page<MonthJiXiaomonthlyAccuratePayVO> monthJiXiaomonthlyAccuratePayVOS1 = (Page<MonthJiXiaomonthlyAccuratePayVO>) monthJiXiaomonthlyAccuratePayVOS;
        for (MonthJiXiaomonthlyAccuratePayVO monthJiXiaomonthlyAccuratePayVO : monthJiXiaomonthlyAccuratePayVOS1) {
            monthJiXiaomonthlyAccuratePayVO.setMonth1(month);
            if (monthJiXiaomonthlyAccuratePayVO.getEmployeeAvatar() != null) {
                String employeeAvatar = monthJiXiaomonthlyAccuratePayVO.getEmployeeAvatar();
                String s = employeeAvatar.split("\", \"name\":")[0];
                String s1 = s.split("\"url\": \"")[1];
                //[{"id": 464, "md5": "67da7d867376f1bb3ae74150f3efc4ca", "uid": "rc-upload-1756280467998-851", "url": "http://171.35.137.170:81/api/whmskj/FZMES/storages/services/StorageSvc/preview?file=67da7d867376f1bb3ae74150f3efc4ca.jpg", "name": "67da7d867376f1bb3ae74150f3efc4ca", "size": 1979860, "type": "image/jpeg", "expires": null, "fileName": "阿布都热木·阿布都尼亚孜.jpg", "fullName": "storages.DiskType", "uploadTime": 1756280469797, "updateUserId": "bcbeb2359b2e4221b854bf087fd7c78b", "storeFullName": null, "retentionPeriod": null}]
                monthJiXiaomonthlyAccuratePayVO.setEmployeeAvatar(s1);
            }
        }
        return new PageResult<>(monthJiXiaomonthlyAccuratePayVOS1.getTotal(),monthJiXiaomonthlyAccuratePayVOS1.getResult());
    }

    /**
     * @param dto
     * @return
     */
    @Override
    public List<MonthJiXiaomonthlyAccuratePayDeVO> monthlyAccuratePayDetail(MonthJiXiaomonthlyAccuratePayDetailDTO dto) {
        String month = dto.getMonth(); // yyyy-MM
        String start;
        String end;
        Boolean isEndDate20th = false;

        LocalDateTime now = LocalDateTime.now();
        YearMonth yearMonth;
        if (month == null || month.isEmpty()) {
            yearMonth = YearMonth.from(now);
        } else {
            yearMonth = YearMonth.parse(month); // 解析 yyyy-MM
        }

// 当前月的20号 21:30
        LocalDateTime curMonth20_2130 = yearMonth.atDay(20).atTime(21, 30);

// 上个月的20号
        LocalDate prevMonth20 = yearMonth.minusMonths(1).atDay(20);

        if (yearMonth.equals(YearMonth.from(now))) {
            // 如果传入的month是当前月
            if (now.isAfter(curMonth20_2130)) {
                // 已经过了本月20号21:30
                end = now.toLocalDate().toString();
                start = yearMonth.atDay(20).toString();
            } else {
                // 还没到本月20号21:30
                end = yearMonth.atDay(20).toString();
                start = prevMonth20.toString();
                isEndDate20th = true;
            }
        } else {
            // 非当前月
            start = prevMonth20.toString();
            end = yearMonth.atDay(20).toString();
            isEndDate20th = true;
        }
        List<MonthJiXiaomonthlyAccuratePayDeVO> monthJiXiaomonthlyAccuratePayDeVOS = gwjemployeeperformanceMapper.monthlyAccuratePayDetail(start, end, dto.getEmployeeId(), isEndDate20th);
        return monthJiXiaomonthlyAccuratePayDeVOS;
    }
}
